﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;


//Hiển thị các giao diện mà khi nhấn vào các giao diện 
//đó sẽ xử lý các sự kiện trong game như di chuyển, dùng skill,...
namespace MyGames
{
    enum EmuButton
    {
        Attack,
        Attack_Double,
        Walk,
        Run,
        UseHealthPotion,
        UseManaPotion,

        UseAvatar,
        UseImpactFlare,
        UseFireShield,
        UseFlameExplosion,
        UseChaoticRift,

        Pause,
        ShowSkillDialog,
        ShowInventory,
        ShowQuestlog
    };

    static class EmuGamePad
    {
        #region Declaration
        static Vector2 dir;
        static PlayerSkillManager skills;

        static Control skillDialog;
        static Control inventoryDialog;
        static Control questDialog;
        static Control pauseGame;

        static Control walk;
        static Control run;
        static Control attack;
        static Control attack_double;
        static Control avatar;
        static Control impactFlare;
        static Control fireShield;
        static Control flameExplosion;
        static Control chaoticRift;

        #endregion

        #region Initalize
        static public void InitEmuGamePad(PlayerSkillManager _skills)
        {
            //skills = skills;

            //skillDialog = new Control(GUI.GamePadGUI.showSkillDialog, GUI.GamePadGUI.showSkillDialogPos, 1f);
            //inventoryDialog = new Control(GUI.GamePadGUI.showInventory, GUI.GamePadGUI.showInventoryPos, 1f);
            //questDialog = new Control(GUI.GamePadGUI.showQuestlog, GUI.GamePadGUI.showQuestlogPos, 1f);
            //pauseGame = new Control(GUI.GamePadGUI.pauseGame, GUI.GamePadGUI.pauseGamePos, 1f);

            //walk = new Control(GUI.GamePadGUI.nagivator, GUI.GamePadGUI.nagivatorPos, 1f);
            ////run = new Control(GUI.GamePadGUI.nagivator, GUI.GamePadGUI.nagivatorPos, 1f);
            attack = new Control(GUI.GamePadGUI.attack, GUI.GamePadGUI.attackPos, 1f);
            ////attack_double = new Control(GUI.GamePadGUI.Attack, GUI.GamePadGUI.attackPos, 1f);
            //avatar = new Control(GUI.GamePadGUI.avatar, GUI.GamePadGUI.avatarPos, 1f);
            //impactFlare = new Control(GUI.GamePadGUI.impactFlare, GUI.GamePadGUI.impactFlarePos, 1f);
            //fireShield = new Control(GUI.GamePadGUI.fireShield, GUI.GamePadGUI.fireShieldPos, 1f);
            //flameExplosion = new Control(GUI.GamePadGUI.flameExplosion, GUI.GamePadGUI.flameExplosionPos, 1f);
            //chaoticRift = new Control(GUI.GamePadGUI.chaoticRift, GUI.GamePadGUI.chaoticRiftPos, 1f);
        }
        #endregion



        #region Public methods
        static public bool IsEmuKeyTriggered(EmuButton btn)
        {
            switch (btn)
            {

                //case EmuButton.Attack:
                //    return InputManager.IsActionTriggered(InputManager.Action.Attack) ||
                //        InputManager.IsButtonClicked(Attack.ButtonRectangle);

                //case EmuButton.UseHealthPotion:
                //    break;

                //case EmuButton.UseManaPotion:
                //    break;

                //case EmuButton.UseAvatar:
                //    return InputManager.IsActionTriggered(InputManager.Action.UseAvatar) ||
                //        InputManager.IsButtonClicked(avatar.ButtonRectangle);

                case EmuButton.UseImpactFlare:
                    return InputManager.IsActionTriggered(InputManager.Action.UseImpactFlare); 
                //||         InputManager.IsButtonClicked(impactFlare.ButtonRectangle);

                //case EmuButton.UseFireShield:
                //    return InputManager.IsActionTriggered(InputManager.Action.UseFireShield) ||
                //        InputManager.IsButtonClicked(fireShield.ButtonRectangle);

                //case EmuButton.UseFlameExplosion:
                //    return InputManager.IsActionTriggered(InputManager.Action.UseFlameExplosion) ||
                //        InputManager.IsButtonClicked(flameExplosion.ButtonRectangle);

                //case EmuButton.UseChaoticRift:
                //    return InputManager.IsActionTriggered(InputManager.Action.UseChaoticRift) ||
                //        InputManager.IsButtonClicked(chaoticRift.ButtonRectangle);

                //case EmuButton.Pause:
                //    return InputManager.IsActionTriggered(InputManager.Action.OpenPauseScreen) ||
                //        InputManager.IsButtonClicked(pauseGame.ButtonRectangle);

                //case EmuButton.ShowSkillDialog:
                //    return InputManager.IsActionTriggered(InputManager.Action.OpenSkillScreen) ||
                //        InputManager.IsButtonClicked(skillDialog.ButtonRectangle);

                //case EmuButton.ShowInventory:
                //    return InputManager.IsActionTriggered(InputManager.Action.OpenInventoryScreen) ||
                //        InputManager.IsButtonClicked(inventoryDialog.ButtonRectangle);

                //case EmuButton.ShowQuestlog:
                //    return InputManager.IsActionTriggered(InputManager.Action.OpenQuestScreen) ||
                //        InputManager.IsButtonClicked(questDialog.ButtonRectangle);
            }

            return false;
        }

        /// <summary>
        /// Xử lý phần thể hiện animation khi một phím được nhấn
        /// </summary>

        static public bool IsEmuKeyPressed(EmuButton btn)
        {
            switch (btn)
            {
                case EmuButton.Attack:
                    return InputManager.IsActionPressed(InputManager.Action.Attack);
                     //   || InputManager.IsButtonHold(Attack.ButtonRectangle);

                case EmuButton.Walk:
                    return (InputManager.IsActionPressed(InputManager.Action.Walk));

                case EmuButton.Run:
                    // return (InputManager.IsButtonHold(run.ButtonRectangle));
                    return (InputManager.IsActionHolded(InputManager.Action.Run));
            }

            return false;
        }

        /// <summary>
        ///  Xử lý phần di chuyển tọa độ khi phím di chuyển được nhấn
        /// </summary>
       
        static public Vector2 GetDirection(GameTime gameTime)
        {
            dir = Vector2.Zero;
#if WINDOWS
            if (InputManager.IsLeftMousePressed())
            {
                dir = InputManager.GetMousePosition() - GUI.GamePadGUI.NagivatorCenter;
                dir.Normalize();
            }

            if (InputManager.IsKeyPressed(Keys.A)) dir.X = -1;
            if (InputManager.IsKeyPressed(Keys.D)) dir.X = 1;
            if (InputManager.IsKeyPressed(Keys.W)) dir.Y = -1;
            if (InputManager.IsKeyPressed(Keys.S)) dir.Y = 1;

            if (InputManager.IsKeyHolded(Keys.Left)) dir.X = -1.8f;
            if (InputManager.IsKeyHolded(Keys.Right)) dir.X = 1.8f;
            if (InputManager.IsKeyHolded(Keys.Up)) dir.Y = -1.8f;
            if (InputManager.IsKeyHolded(Keys.Down)) dir.Y = 1.8f;

            if (InputManager.IsKeyPressedCombo(Keys.S, Keys.D))
            {
                dir.Y = 1;
                dir.X = 1;
            }
            if (InputManager.IsKeyHolded(Keys.Right)) dir.X = 1.8f;
            if (InputManager.IsKeyHolded(Keys.Up)) dir.Y = -1.8f;
            if (InputManager.IsKeyHolded(Keys.Down)) dir.Y = 1.8f;

            if (InputManager.IsKeyPressedLongTime(Keys.Right, gameTime)) dir.X = 2;
#else
            if (InputManager.CurrentTouchState.Count > 0)
            {
                dir = InputManager.CurrentTouchState[InputManager.CurrentTouchState.Count - 1].Position -
                    GUI.GamePadGUI.NagivatorCenter;
                dir.Normalize();
            }
#endif
            return dir;
        }
        #endregion

        #region Draw and Update
        static public void Draw(SpriteBatch spriteBatch)
        {
            //walk.Draw(spriteBatch, null, Color.White, SpriteEffects.None);
            ////run.Draw(spriteBatch, null, Color.White, SpriteEffects.None);
            attack.Draw(spriteBatch, null, Color.White, SpriteEffects.None);

            //skillDialog.Draw(spriteBatch, null, Color.White, SpriteEffects.None);
            //inventoryDialog.Draw(spriteBatch, null, Color.White, SpriteEffects.None);
            //questDialog.Draw(spriteBatch, null, Color.White, SpriteEffects.None);
            //pauseGame.Draw(spriteBatch, null, Color.White, SpriteEffects.None);

            //if (skills.skills[0].Avaiable) avatar.Draw(spriteBatch, null, Color.White, SpriteEffects.None);
            //if (skills.skills[1].Avaiable) impactFlare.Draw(spriteBatch, null, Color.White, SpriteEffects.None);
            //if (skills.skills[3].Avaiable) fireShield.Draw(spriteBatch, null, Color.White, SpriteEffects.None);
            //if (skills.skills[4].Avaiable) flameExplosion.Draw(spriteBatch, null, Color.White, SpriteEffects.None);
            //if (skills.skills[5].Avaiable) chaoticRift.Draw(spriteBatch, null, Color.White, SpriteEffects.None);
        }
        #endregion
    }

    
}
